package calendar;

import calendar.messages.*;
import edu.gmu.middleware.Message;
import edu.gmu.middleware.RemoteStub;
import java.io.IOException;
import java.util.ArrayList;

public class CalendarMgrStub implements CalendarMgrInt
{
    private RemoteStub         stub = null;
    
    /**
     * Creates a new CalendarStub with specified params.
     */
    public CalendarMgrStub( String host,
                            int port,
                            int id,
                            String name ) throws IOException
    {
        this.stub        = new RemoteStub( host, port, id, name );

        this.stub.register(Topic.CREATE_CALENDAR_RESPONSE, Result.class);
        this.stub.register(Topic.LIST_RESPONSE,       ListResponse.class);
        this.stub.register(Topic.CONNECT_RESPONSE,    ConnectResponse.class);
        this.stub.register(Topic.DISCONNECT_RESPONSE, DisconnectResponse.class);
        this.stub.register(Topic.ALIVE_RESPONSE,      AliveResponse.class);
    }

    public boolean createCalendar(User user) throws IOException
    {
        Message message = this.stub.execute(Topic.CREATE_CALENDAR_REQUEST, user);
        Result result = (Result)message;
        return result.result;
    }

    public boolean connectCalendar(User user) throws IOException
    {
        ConnectRequest request = new ConnectRequest();
        request.user = user;
        Message message = this.stub.execute(Topic.CONNECT_REQUEST, request);
        ConnectResponse response = (ConnectResponse)message;
        return response.result;
    }

    public boolean disconnectCalendar(User user) throws IOException
    {
        DisconnectRequest request = new DisconnectRequest();
        request.user = user;
        Message message = this.stub.execute(Topic.DISCONNECT_REQUEST, request);
        DisconnectResponse response = (DisconnectResponse)message;
        return response.result;
    }

    public ArrayList list() throws IOException
    {
        ListRequest request = new ListRequest();
        Message message = this.stub.execute(Topic.LIST_REQUEST, request);
        ListResponse response = (ListResponse)message;
        return response.getUsers();
    }

    public void isAlive() throws IOException {
        AliveRequest request = new AliveRequest();
        Message message = this.stub.execute(Topic.ALIVE_REQUEST, request);
        AliveResponse response = (AliveResponse)message;
        return;
    }
}
